package com.yoga.manager.app.impl;

import com.yoga.common.appexception.AppErrorCodeEnum;
import com.yoga.common.appexception.AppException;
import com.yoga.common.bo.CourseBO;
import com.yoga.common.page.PageBO;
import com.yoga.common.qto.CourseQTO;
import com.yoga.dao.dao.CourseDao;
import com.yoga.manager.app.AppCourseManager;
import com.yoga.manager.beanUtil.CourseBeanUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by hdx on 2020/9/29.
 */
@Component
public class AppCourseManagerImpl implements AppCourseManager {

    @Resource
    private CourseDao courseDao;

    @Override
    public PageBO<CourseBO> findForPage(CourseQTO courseQTO) throws AppException {
        PageBO<CourseBO> pageBO = new PageBO<>();
        if (courseQTO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (courseQTO.getYogaShopId() == null) {
            throw new AppException(AppErrorCodeEnum.NOT_SHOP_CHORES);
        }
        pageBO.setQuery(courseQTO);
        Integer count = courseDao.findForPageCount(courseQTO);
        if (count <= 0) {
            return pageBO;
        }
        List<CourseBO> courseBOs = CourseBeanUtil.doList2BOList(courseDao.findForPage(courseQTO));
        pageBO.setTotalCount(count);
        pageBO.setData(courseBOs);

        return pageBO;
    }
}
